﻿using Mogubang_Sl.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogubang_Sl.Core;

namespace Mogubang_Sl.Utils
{
    public class MoguDataHelper
    {
        /// <summary>
        /// 游戏数据转换成保存的数据结构
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static Dictionary<int, MoguData> MoguDataToDictionary(MoguData[,] datas)
        {
            Dictionary<int, MoguData> result = new Dictionary<int, MoguData>();

            for (int i = 0; i < GameStatics.RowColumnCount; i++)
            {
                for (int j = 0; j < GameStatics.RowColumnCount; j++)
                {
                    result.Add(i * GameStatics.RowColumnCount + j, datas[i, j]);
                }
            }

            return result;
        }

        /// <summary>
        /// 保存的数据转成游戏数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static MoguData[,] DictionaryToMoguData(Dictionary<int, MoguData> data)
        {
            MoguData[,] result = new MoguData[GameStatics.RowColumnCount, GameStatics.RowColumnCount];

            foreach (var index in data.Keys)
            {
                int row = index / GameStatics.RowColumnCount;
                int column = index % GameStatics.RowColumnCount;

                result[row, column] = data[index];
            }

            return result;
        }

        /// <summary>
        /// 获取获得的积分
        /// </summary>
        /// <param name="level"></param>
        /// <param name="count"></param>
        /// <param name="combo"></param>
        /// <returns></returns>
        public static int GetScore(int level, int count, int combo)
        {
            return (count - 2) * (int)Math.Pow(2, level) * combo;
        }

        /// <summary>
        /// 获取非爱心模块消除的时候的获取的爱心数量
        /// </summary>
        /// <param name="count"></param>
        /// <param name="combo"></param>
        /// <returns></returns>
        public static int GetNewLife(int count, int combo)
        {
            return (count - 3 + (combo - 1)) * (int)Math.Sqrt(combo);
        }

        /// <summary>
        /// 获取消除爱心时获取的爱心数量
        /// </summary>
        /// <param name="count"></param>
        /// <param name="combo"></param>
        /// <returns></returns>
        public static int GetNewLifeEliminate(int count, int combo)
        {
            return (int)(Math.Sqrt(count - 2) * 5 * Math.Sqrt(combo));
        }

        /// <summary>
        /// 根据最终得分获取金币
        /// </summary>
        /// <param name="score"></param>
        /// <returns></returns>
        public static int GetAddCoinCount(int score)
        {
            return score / 10;
        }
    }
}
